import axios from '../plugins/axios'
import store from '../service/store'
import qs from 'qs'
let serverip = 'http://39.105.42.134:17001/manager'
// let serverip = 'http://127.0.0.1:6370'

let api = {
  login: serverip + '/login',
  refresh_token: serverip + '/refresh',
  github_login: serverip + '/github_login',
  github_auth: 'https://github.com/login/oauth/authorize?client_id=9304cb97ee37d73f175c&redirect_uri=http://127.0.0.1:8080/auth',
  current_user: () => {
    return axios.get(serverip + '/auth/user').then(({data}) => {
      if (data.ret === 1) {
        store.commit('setUserInfo', {
          username: data.result.username,
          phone: data.result.phone,
          id: data.result.id,
          email: data.result.email
        })
      }
      return data
    })
  },
  menu_list: () => {
    return axios.get(serverip + '/auth/menu').then(({data}) => {
      if (data.ret === 1) {
        store.commit('setMenus', data.result)
      }
      return data
    })
  },
  commodityRootClassfication: () => {
    return axios.get(serverip + '/classification/root').then(({data}) => {
      return data
    })
  },
  commodityChildClassfication: (id) => {
    return axios.get(serverip + '/classification/' + id + '/child').then(({data}) => {
      return data
    })
  },
  commodityClassficationList: () => {
    return axios.get(serverip + '/classifications').then(({data}) => {
      return data
    })
  },
  addCommodityClassfication: (data) => {
    return axios.post(serverip + '/classifications', qs.stringify(data)).then(({data}) => {
      return data
    })
  },
  deleteCommodityClassfication: (id) => {
    return axios.delete(serverip + '/classifications/' + id).then(({data}) => {
      return data
    })
  },
  roleList: (data) => {
    return axios.get(serverip + '/role?' + qs.stringify(data)).then(({data}) => {
      return data
    })
  },
  rolePermissions: (id) => {
    return axios.get(serverip + '/role/' + id + '/permissions').then(({data}) => {
      return data
    })
  },
  editRole: (data) => {
    return axios.post(serverip + '/role/' + data.id, qs.stringify(data, { indices: false })).then(({data}) => {
      return data
    })
  },
  addRole: (data) => {
    return axios.post(serverip + '/role', qs.stringify(data, { indices: false })).then(({data}) => {
      return data
    })
  },
  deleteRole: (id) => {
    return axios.delete(serverip + '/role/' + id).then(({data}) => {
      return data
    })
  },
  permissionsList: () => {
    return axios.get(serverip + '/permissions').then(({data}) => {
      return data
    })
  },
  staffList: (data) => {
    return axios.get(serverip + '/staff?' + qs.stringify(data)).then(({data}) => {
      return data
    })
  },
  addStaff: (data) => {
    return axios.post(serverip + '/staff', qs.stringify(data, { indices: false })).then(({data}) => {
      return data
    })
  },
  staffDetail: (id) => {
    return axios.get(serverip + '/staff/' + id).then(({data}) => {
      return data
    })
  },
  editStaff: (id, data) => {
    return axios.post(serverip + '/staff/' + id, qs.stringify(data, { indices: false })).then(({data}) => {
      return data
    })
  },
  deleteStaff: (id) => {
    return axios.delete(serverip + '/staff/' + id).then(({data}) => {
      return data
    })
  },
  roleSuggest: (data) => {
    return axios.get(serverip + '/role/suggest?' + qs.stringify(data, { indices: false })).then(({data}) => {
      return data
    })
  },
  changePassword: (id, data) => {
    return axios.post(serverip + '/staff/' + id + '/changePassword', qs.stringify(data, { indices: false })).then(({data}) => {
      return data
    })
  },
  commodityList: (data) => {
    return axios.get(serverip + '/commodity?' + qs.stringify(data, { indices: false })).then(({data}) => {
      return data
    })
  },
  addCommodity: (data) => {
    return axios.post(serverip + '/commodity', qs.stringify(data, { indices: false })).then(({data}) => {
      return data
    })
  },
  commodityInfo: (id) => {
    return axios.get(serverip + '/commodity/' + id).then(({data}) => {
      return data
    })
  },
  editCommodity: (id, data) => {
    return axios.post(serverip + '/commodity/' + id, qs.stringify(data, { indices: false })).then(({data}) => {
      return data
    })
  },
  onSaleCommodity: (id) => {
    return axios.post(serverip + '/commodity/' + id + '/onSale').then(({data}) => {
      return data
    })
  },
  unSaleCommodity: (id) => {
    return axios.post(serverip + '/commodity/' + id + '/unSale').then(({data}) => {
      return data
    })
  },
  addCommoditySku: (data) => {
    return axios.post(serverip + '/commoditysku', data).then(({data}) => {
      return data
    })
  },
  upLoadImg: (data) => {
    return axios.post(serverip + '/file/img/upload', data).then(({data}) => {
      return data
    })
  },
  flashPromotionList: (data) => {
    return axios.get(serverip + '/flashPromotion?' + qs.stringify(data)).then(({data}) => {
      return data
    })
  },
  flashPromotionSessionList: () => {
    return axios.get(serverip + '/flashPromotionSession/all').then(({data}) => {
      return data
    })
  },
  flashPromotionCommodityList: (data) => {
    return axios.get(serverip + '/flashPromotionCommodity?' + qs.stringify(data)).then(({data}) => {
      return data
    })
  },
  addFlashPromotionList: (data) => {
    return axios.post(serverip + '/flashPromotion', qs.stringify(data)).then(({data}) => {
      return data
    })
  },
  getPermissions: () => {
    return axios.get(serverip + '/permissions').then(({data}) => {
      return data
    })
  },
  addPermissions: (data) => {
    return axios.post(serverip + '/permissions?' + qs.stringify(data)).then(({data}) => {
      return data
    })
  },
  findFlashPromotionSessionList: (data) => {
    return axios.get(serverip + '/flashPromotionSession?' + qs.stringify(data)).then(({data}) => {
      return data
    })
  }
}
export default api
